
<template>
  <el-dialog v-model="ifShow" title="配置" width="70rem" :before-close="handleCancel">
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="auto" class="demo-ruleForm" status-icon>
      <el-form-item label="城市" prop="cityId">
        <el-select style="width: 40rem" filterable clearable v-model="formData.cityId" placeholder="请选择">
          <el-option v-for="item in cityOptions" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="级别配置">
        <div class="flex_box" v-for="(item, i) in configArr" :key="item.id">
          <el-input placeholder="请输入级别" v-model="item.grade"></el-input>
          <el-input placeholder="请输入延迟时间" v-model="item.time" suffix="">
            <template #append>秒</template>
          </el-input>
          <el-input placeholder="请输入完单量配置" v-model="item.num"></el-input>
          <el-input placeholder="请输入配置名称" v-model="item.name"></el-input>
          <el-button type="danger" @click="handleRemove(i)">删除</el-button>
          <el-button style="position: absolute; right: -5rem; bottom: 0" v-if="i == configArr.length - 1" type="primary" @click="handleAdd">新增</el-button>
        </div>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" @click="handleOk">确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, onMounted, reactive } from "vue";
import CommonApi from "@/api/common";
import JntApi from "@/api/jnt";
import Supplier from "@/api/supplier";
import { channelArr, bizTypeArr, orderTypeArr, platFormArr, carTypeArr } from "@/enums/common";
import DriverApi from "@/api/driver";

const props = defineProps<{}>();
const emit = defineEmits(["success"]);
const formRef: any = ref(null);
const cityOptions = ref();
const formData: any = reactive({
  id: "",
  status: undefined,
  billingTime: "",
  strategy: "",
});
const rules = reactive({
  strategy: [{ required: true, message: "请选择!" }],
  status: [{ required: true, message: "请选择!" }],
  billingTime: [{ required: true, message: "请选择!" }],
});
const configArr: any = ref([{ grade: "", time: "", num: "", name: "", id: Date.now() + Math.random().toString(16).slice(2) }]);
const ifShow = ref(false);
function edit(row: any) {
  ifShow.value = true;
  if (row.id) {
    // 里程价格
    if (row.gradeDTO) {
      configArr.value = row.gradeDTO;
    }
  }
  //赋值
  Object.assign(formData, row);
}

function handleRemove(index) {
  if (configArr.value.length !== 1) {
    configArr.value.splice(index, 1);
  }
}
function handleAdd() {
  configArr.value.push({ grade: "", time: "", num: "", name: "", id: Date.now() + Math.random().toString(16).slice(2) });
}

/**
 * 确定按钮点击事件
 */
async function handleOk() {
  await formRef.value.validate((valid: any, fields: any) => {
    if (valid) {
      let model: any = formData;
      model.gradeDTO = configArr.value;
      DriverApi.driverRankEdit(model).then((res: any) => {
        if (res.code == 0) {
          ElMessage.success(res.message);
          formRef.value.resetFields();
          emit("success");
          handleCancel();
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
}

/**
 * 取消按钮回调事件
 */
function handleCancel() {
  formRef.value.resetFields();
  configArr.value = [{ grade: "", time: "", num: "", name: "", id: Date.now() + Math.random().toString(16).slice(2) }];
  ifShow.value = false;
}

defineExpose({
  edit,
});
onMounted(() => {
  CommonApi.getCity({ pageNo: 1, pageSize: 9999 }).then((data) => {
    if (data.code == 0) {
      cityOptions.value = data.result;
    }
  });
});
</script>
<style lang="scss" scoped>
.flex_box {
  display: flex;
  margin-bottom: 14px;
  position: relative;
}
</style>
